package cn.tedu.property_management_system.service.Impl;

import cn.tedu.property_management_system.cache.repo.IRedisCache;
import cn.tedu.property_management_system.entity.Parking;
import cn.tedu.property_management_system.entity.VO.OwnerVO;
import cn.tedu.property_management_system.entity.VO.ParkingVO;
import cn.tedu.property_management_system.entity.dto.ParkingDAV;
import cn.tedu.property_management_system.entity.dto.ParkingDTO;
import cn.tedu.property_management_system.ex.ServiceException;
import cn.tedu.property_management_system.mapper.OwnerMapper;
import cn.tedu.property_management_system.mapper.ParkingMapper;
import cn.tedu.property_management_system.service.IParkingService;
import cn.tedu.property_management_system.utils.JsonPage;
import cn.tedu.property_management_system.utils.JsonResult;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.parameters.P;
import org.springframework.stereotype.Service;

import javax.print.PrintService;
import java.io.Serializable;
import java.security.SecureRandom;
import java.util.ArrayList;
import java.util.List;

@Service
@Slf4j
public class ParkingServiceImpl implements IParkingService {

    @Autowired
    private ParkingMapper parkingMapper;

    @Autowired
    private OwnerMapper ownerMapper;

    @Override
    public ParkingVO getByPosition(String position) {
        String key = "parking" + position;
        Serializable serializable = redisCache.getACache(key);
        if (serializable != null) {
            if (serializable instanceof ParkingVO)
                return (ParkingVO) serializable;
        }
        boolean isKey = redisCache.hasKey(key);
        if (isKey) {
            throw new ServiceException(JsonResult.State.ERR_USERNAME, "该数据不存在");
        }
        ParkingVO parkingVO = parkingMapper.getByPosition(position);
        if (parkingVO == null) {
            redisCache.addACache(key, null, 5);
            throw new ServiceException(JsonResult.State.ERR_USERNAME, "该数据不存在");
        }

        return parkingVO;
    }

    @Override
    public List<ParkingVO> getParking() {

        List<ParkingVO> parkingVOS = getParkingVOS();

        return parkingVOS;
    }

    @Override
    public int insertParking(ParkingDTO parkingDTO) {
        ParkingVO parkingVO = parkingMapper.getByPosition(parkingDTO.getPosition());
        if (parkingVO != null) {
            throw new ServiceException(JsonResult.State.ERR_USERNAME, "该车位已存在");
        }
        OwnerVO ownerVO = ownerMapper.getByName(parkingDTO.getOwnerName());
        if (ownerVO == null) {
            throw new ServiceException(JsonResult.State.ERR_USERNAME, "户主不存在");
        }
        Parking parking = new Parking();
        BeanUtils.copyProperties(parkingDTO, parking);
        parking.setOwnerId(ownerVO.getId());
        parking.setTime1(System.currentTimeMillis());
        int row = parkingMapper.insertParking(parking);
        if (row == 0) {
            throw new ServiceException(JsonResult.State.ERR_USERNAME, "插入数据失败");
        }
        loadCache();
        return row;
    }

    @Override
    public int updateParking(ParkingDAV parkingDAV) {
        ParkingVO parkingVO = parkingMapper.getById(parkingDAV.getId());
        if (parkingVO == null) {
            throw new ServiceException(JsonResult.State.ERR_USERNAME, "该数据不存在");
        }
        OwnerVO ownerVO = ownerMapper.getByName(parkingDAV.getOwnerName());
        if (ownerVO == null) {
            throw new ServiceException(JsonResult.State.ERR_USERNAME, "户主不存在");
        }
        Parking parking = new Parking();
        BeanUtils.copyProperties(parkingDAV, parking);
        parking.setOwnerId(ownerVO.getId());
        parking.setTime1(System.currentTimeMillis());
        int row = parkingMapper.updateParking(parking);
        if (row == 0) {
            throw new ServiceException(JsonResult.State.ERR_USERNAME, "修改数据失败");
        }
        loadCache();
        return row;
    }

    @Override
    public int deleteParking(Integer id) {
        ParkingVO parkingVO = parkingMapper.getById(id);
        if (parkingVO == null) {
            throw new ServiceException(JsonResult.State.ERR_USERNAME, "该数据不存在");
        }
        int row = parkingMapper.deleteParking(id);
        if (row == 0) {
            throw new ServiceException(JsonResult.State.ERR_USERNAME, "删除数据失败");
        }
        loadCache();
        return row;
    }

    @Override
    public int deleteParkings(List<ParkingDAV> list) {
        for (ParkingDAV parkingDAV : list) {
            parkingMapper.deleteParking(parkingDAV.getId());
        }
        loadCache();
        return 1;
    }

    @Override
    public void loadParking() {
        loadCache();
    }

    @Override
    public ParkingVO getById(Integer id) {
        ParkingVO parkingVO = parkingMapper.getById(id);
        return parkingVO;
    }

    @Override
    public List<String> getPosition() {
        List<ParkingVO> parkingVOS = parkingMapper.getListParking();
        List<String> list = new ArrayList<>();
        for (ParkingVO parkingVO : parkingVOS) {
            list.add(parkingVO.getPosition());
        }
        loadCache();
        return list;
    }

    @Autowired
    private IRedisCache redisCache;


    private void loadCache() {
        String key = "parkings";
        List<ParkingVO> parkingVOS = parkingMapper.getListParking();
        List<Serializable> serializables = new ArrayList<>();
        for (ParkingVO parkingVO : parkingVOS) {
            serializables.add(parkingVO);
        }
        redisCache.clear(key);
        redisCache.addCaches(key, serializables);
    }

    private List<ParkingVO> getParkingVOS() {
        String key = "parkings";
        List<Serializable> serializables = redisCache.getCaches(key);
        List<ParkingVO> parkingVOS = new ArrayList<>();
        for (Serializable serializable : serializables) {
            parkingVOS.add((ParkingVO) serializable);
        }
        return parkingVOS;
    }


    public JsonPage<ParkingVO> pageSelect(Integer pageSize, Integer pageIndex){
        PageHelper.startPage(pageSize,pageIndex);
        List<ParkingVO> list=parkingMapper.getListParking();
        return JsonPage.restPage(new PageInfo<>(list));
    }



}
